package com.base_data_handler.sx.handler;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Logger;

import org.apache.commons.dbutils.QueryRunner;

import com.base_data_handler.utils.DataSourceUtils;
import com.base_data_handler.utils.DruidPlugin;
import com.base_data_handler.utils.PinyinUtil;
import com.base_data_handler.utils.PropertiesUtil;
import com.base_data_handler.utils.ToolDateTime;
import com.base_data_handler.utils.ftp.FtpUtil;
/**
 * cimmiss  服务产品 上传监控 每2分钟执行
 * @param urlStr
 * @return
 */
public class SX_FileUpload_Handler  {
	private	static QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	private static final Logger log = Logger.getLogger(SX_FileUpload_Handler.class.getName());
	/**
	 * 创建文件存放路径
	 */
	public	static  void createDir() {
		String arr[]=new String[]{"西安:临潼区,周至县,户县,蓝田县,西安市,长安区,高陵县,新城区,碑林区,莲湖区,灞桥区,未央区,雁塔区,阎良区",
								  "铜川:王益区,耀州区,宜君县,印台区",
								  "宝鸡:渭滨区,陈仓区,凤翔县,岐山县,扶风县,眉县,陇县,千阳县,麟游县,凤县,太白县,金台区",
								  "咸阳:三原县,乾县,兴平市,彬县,旬邑县,杨凌区,武功县,永寿县,泾阳县,淳化县,渭城区,礼泉县,长武县,秦都区",
								  "渭南:临渭区,华县,华阴市,合阳县,大荔县,富平县,潼关县,澄城县,白水县,蒲城县,韩城市",
								  "延安:吴起县,子长县,安塞县,宜川县,宝塔区,富县,延川县,延长县,志丹县,洛川县,甘泉县,黄陵县,黄龙县",
								  "汉中:佛坪县,勉县,南郑县,城固县,宁强县,汉台区,洋县,留坝县,略阳县,西乡县,镇巴县",
								  "榆林:佳县,吴堡县,子洲县,定边县,府谷县,榆阳区,横山县,清涧县,神木县,米脂县,绥德县,靖边县",
								  "安康:宁陕县,岚皋县,平利县,旬阳县,汉滨区,汉阴县,白河县,石泉县,紫阳县,镇坪县",
								  "商洛:丹凤县,商南县,商州区,山阳县,柞水县,洛南县,镇安县"};
		String arrFile[]=new String []{"决策","农业气象","旅游","环境","森林防火"};
		String dir="";
		File fileDir=null;
		for (int i = 0; i < arr.length; i++) {
			String city=arr[i].split(":")[0];
			String cntys[]=arr[i].split(":")[1].split(",");
			for (int j = 0; j < cntys.length; j++) {
				//city=city+cntys[j];
				for (int k = 0; k < arrFile.length; k++) {
					dir=PropertiesUtil.FTP_RESOURCELOCAL+"/"+city+"/"+cntys[j]+"/"+arrFile[k];
					fileDir=new File(PinyinUtil.toHanyuPinyin(dir));
					if(!fileDir.exists()){
						boolean ok = fileDir.mkdirs();
						if(ok){
							System.out.println("创建成功"+dir);
						}else{
							System.out.println("创建失败"+dir);
						}
					}else{
						System.out.println("目录已存在"+dir);
					}
				}
			}
		}
    }
	
	public static void listenerFileDir() throws IOException, SQLException{
		
		File cityDir=new File(PropertiesUtil.FTP_RESOURCELOCAL);
		File[] cityroots = cityDir.listFiles();
		
		//获取市级目录
		for (int i = 0; i < cityroots.length; i++) {
			
			File[] listCnty = cityroots[i].listFiles();
			//获取县级目录
			for (int j = 0; j < listCnty.length; j++) {
				File[] listProduct = listCnty[j].listFiles();
				//获取产品目录
				for (int k = 0; k < listProduct.length; k++) {
					
					File[] pdf = listProduct[k].listFiles();
					for (int l = 0; l < pdf.length; l++) {
//						System.out.println(pdf[l].getParent().replace("D:\\product", ""));
//						FtpUtil.upload(pdf[l].getParent().replace("D:\\", ""), PropertiesUtil.FTP_USERNAME, PropertiesUtil.FTP_PASSWORD,
//								PropertiesUtil.FTP_HOST, PropertiesUtil.FTP_PORT, pdf[l].getAbsolutePath(),null, pdf[l].getName());
						String time=ToolDateTime.format(new Date(pdf[l].lastModified()), "yyyy-MM-dd HH:mm:ss");
						boolean isok = FtpUtil.upload(PinyinUtil.toHanyuPinyin(pdf[l].getParent().replace("D:\\", "")), PropertiesUtil.FTP_USERNAME, PropertiesUtil.FTP_PASSWORD,
								PropertiesUtil.FTP_HOST, PropertiesUtil.FTP_PORT, pdf[l].getAbsolutePath());
						if(isok){
							System.out.println( pdf[l].getAbsolutePath());
							saveRecord2db(pdf[l],time);
						}
					}
					
				}
			}
			
		}
	}
	
	
	public static void saveRecord2db(File filePtah,String time) throws SQLException{
		String title=filePtah.getName().split("\\.")[0];
		String arr[]=filePtah.getPath().split("\\\\");
		String sql="insert into d_server_ptoduct (title,position,first_class,second_class,file_path,product_type,up_time) "
				+ " values(?,?,?,?,?,?,?)";
		Object[][] params=new Object[1][7];
		
		if(arr.length>5){
			params[0][0]=title;
			params[0][1]=arr[3];
			params[0][2]="气象服务";
			params[0][3]=arr[4];
			params[0][4]=PinyinUtil.toHanyuPinyin(arr[2]+"/"+arr[3]+"/"+arr[4]+"/");
			params[0][5]="pdf";
			params[0][6]=time;
			 int[] i = qr.batch(sql, params);
		}
			System.out.println(""+arr[2]+"市"+arr[3]+"气象服务"+arr[4]+arr[5]+filePtah.lastModified());
//		title,content,position,first_class,second_class,file_path,product_type,up_time

		
		
	}
	public static void main(String[] args) throws Exception {
//		while (true) {
			SX_FileUpload_Handler.listenerFileDir();
//			SX_FileUpload_Handler.createDir();
			
//			saveRecord2db(new File("D:\\product\\榆林\\子洲县\\农业气象\\Android笔试题答案.doc"));
			//Thread.sleep(15000);
//		}
	}
	
}
